﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Phone.FlexGrid;

namespace ControlExplorer.Controls.FlexGrid
{
    public partial class FlexGridDemo : UserControl
    {
        public FlexGridDemo()
        {
            InitializeComponent();
            SetupGrid();
        }

        private void SetupGrid()
        {
            // create the data
            var data = Customer.GetCustomerList(100);
            flexgrid.ItemsSource = data;

            // hide read-only "Country" column 
            var col = flexgrid.Columns["Country"];
            col.Visible = false;

            // map countryID column so it shows country names instead of their IDs
            Dictionary<int, string> dct = new Dictionary<int, string>();
            foreach (var country in Customer.GetCountries())
            {
                dct[dct.Count] = country;
            }
            col = flexgrid.Columns["CountryID"];
            col.ValueConverter = new ColumnValueConverter(dct);
            col.HorizontalAlignment = HorizontalAlignment.Left;
            col.Header = "Country";
            col.Width = new GridLength(140);

            // provide auto-complete lists for first and last name columns
            col = flexgrid.Columns["First"];
            col.ValueConverter = new ColumnValueConverter(Customer.GetFirstNames(), false);
            col = flexgrid.Columns["Last"];
            col.ValueConverter = new ColumnValueConverter(Customer.GetLastNames(), false);

            col = flexgrid.Columns["Hired"];
            col.Width = new GridLength(140);

            col = flexgrid.Columns["Weight"];
            col.Width = new GridLength(100);

            col = flexgrid.Columns["Active"];
            col.Width = new GridLength(100);


            // make read-only columns look disabled
            foreach (var c in flexgrid.Columns)
            {
                if (c.PropertyInfo != null && !c.PropertyInfo.CanWrite)
                {
                    c.Foreground = new SolidColorBrush(Colors.Gray);
                }
            }
        }
    }
}
